package com.chen.pspms.service.impl;

import com.chen.pspms.entity.User;
import com.chen.pspms.mapper.UserMapper;
import com.chen.pspms.service.UserService;
import com.chen.pspms.utils.ApplicationException;
import com.chen.pspms.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Integer save(User user) throws ApplicationException {
        int row=0;
        try {
            row = userMapper.save(user);
        } catch (Exception e) {
            throw new ApplicationException("新增用户失败", e);
        }
        return row;
    }

    @Override
    public Integer delete(User user) throws ApplicationException {
        int row=0;
        try {
            row = userMapper.delete(user);
        } catch (Exception e) {
            throw new ApplicationException("删除用户失败", e);
        }
        return row;
    }

    @Override
    @RedisCache(key = "#id")
    public User findyById(Integer id) throws ApplicationException {
        User user;
        try {
            user = userMapper.findyById(id);
        } catch (Exception e) {
            throw new ApplicationException("查询用户失败", e);
        }
        return user;
    }

    @Override
    @RedisCache(key = "#account")
    public User findyByAccount(String account) throws ApplicationException {
        User user;
        try {
            user = userMapper.findyByAccount(account);
        } catch (Exception e) {
            throw new ApplicationException("查询用户失败", e);
        }
        return user;
    }

    @Override
    public Integer update(User user) throws ApplicationException {
        int row=0;
        try {
            row = userMapper.update(user);
        } catch (Exception e) {
            throw new ApplicationException("更新用户失败", e);
        }
        return row;
    }

    @Override
    public List<User> findyAll() throws ApplicationException {
        List<User> holderlist;
        try {
            holderlist = userMapper.findyAll();
        } catch (Exception e) {
            throw new ApplicationException("查询用户列表失败", e);
        }
        return holderlist;
    }
}
